package com.redwine.app;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.redwine.app.adapter.SupplierTypeAdapter;
import com.redwine.app.net.GsonRequest;
import com.redwine.app.net.NetworkConstants;
import com.redwine.app.net.responses.RedwineDetailResponse;
import com.redwine.app.net.responses.SendMsgEvent;
import com.redwine.app.net.responses.SupplierTypeCustom;
import com.redwine.app.net.responses.SupplierTypeResponse;
import com.redwine.app.utils.NetUtil;
import com.redwine.app.view.EmptyView;
import com.redwine.app.view.MeasureGridView;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

import static com.redwine.app.utils.Utils.showToast;

/**
 * 申请成为供应商
 * 作者：gassion on 17/8/7 22:46
 * 最可怕的敌人，就是没有坚强的信念。
 */
public class ApplyForSupplierActivity extends BaseActivity {

    @ViewInject(R.id.header_title)
    private TextView headerTitle;
    @ViewInject(R.id.layout_back)
    private LinearLayout layoutBack;
    @ViewInject(R.id.supplier_ll_all)
    private LinearLayout layoutAll;
    @ViewInject(R.id.empty_view)
    private EmptyView emptyView;
    public static final String PUBLISH_ENTITY = "publishEntity";
    private RedwineDetailResponse.DataEntity.PublishEntity publishEntity;
    @ViewInject(R.id.supplier_tv_measure_grid_view)
    private MeasureGridView supplierTvMeasureGridView;
    private SupplierTypeAdapter supplierTypeAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply_for_supplier);
        ViewUtils.inject(this);
        EventBus.getDefault().register(this);
        initView();
        requestData();
    }

    private void initView() {
        Intent intent = getIntent();
        if (null != intent) {
            publishEntity = (RedwineDetailResponse.DataEntity.PublishEntity) intent.getSerializableExtra(PUBLISH_ENTITY);
        }
        headerTitle.setText(R.string.supplier_certification);
        layoutBack.setVisibility(View.VISIBLE);
        supplierTypeAdapter = new SupplierTypeAdapter(ApplyForSupplierActivity.this);
        supplierTvMeasureGridView.setAdapter(supplierTypeAdapter);
        setHiddenView(emptyView);
    }

    @OnClick({R.id.layout_back, R.id.supplier_tv_next, R.id.supplier_tv_instructions})
    public void mClick(View v) {
        switch (v.getId()) {

            case R.id.layout_back:
                finish();
                break;

            case R.id.supplier_tv_next:
                submit();
                break;

            case R.id.supplier_tv_instructions:
                Intent intent = new Intent(ApplyForSupplierActivity.this, CommonWebViewActivity.class);
                intent.putExtra(CommonWebViewActivity.TITLE_PARAMS, getString(R.string.supplier_agreement));
                String url = NetworkConstants.WEB_SUPPLIER_URL;
                intent.putExtra(CommonWebViewActivity.LOAD_URL, url);
                startActivity(intent);
                break;

        }
    }

    private void submit() {
        StringBuilder stringBuilder = new StringBuilder();
        List<SupplierTypeCustom> supplierTypeAdapterData = supplierTypeAdapter.getData();
        for (int i = 0; i < supplierTypeAdapterData.size(); i++) {
            SupplierTypeCustom supplierTypeCustom = supplierTypeAdapterData.get(i);
            if (supplierTypeCustom.isSelect()) {
                stringBuilder.append(supplierTypeCustom.getType() + ",");
            }
        }

        if (TextUtils.isEmpty(stringBuilder.toString())) {
            showToast(getString(R.string.choose_the_business_type));
            return;
        }
        String typeString = stringBuilder.toString().substring(0, stringBuilder.toString().length() - 1);
        publishEntity.setSupplierType(typeString);

        Intent intent = new Intent(ApplyForSupplierActivity.this, ApplyForSupplierNextActivity.class);
        intent.putExtra(ApplyForSupplierNextActivity.PUBLISH_ENTITY, publishEntity);
        startActivity(intent);
    }


    /**
     * 认证运营商业务类型
     */
    private void requestData() {
        if (!NetUtil.checkNet(this)) {
            showToast(R.string.generic_check);
            setErrorView(emptyView, null);
            return;
        }
        showWaiting();
        String url = NetworkConstants.SUPPLIER_GET_TYPE_URL;
        Map<String, String> params = new LinkedHashMap<>();
        params.put("uid", publishEntity.getUid());
        GsonRequest<SupplierTypeResponse> request = new GsonRequest<>(url, Request.Method.POST,
                SupplierTypeResponse.class, false, params, new Response.Listener<SupplierTypeResponse>() {
            @Override
            public void onResponse(SupplierTypeResponse response) {
                dismissWaiting();
                if (response.isSuccess()) {
                    SupplierTypeResponse.DataEntity responseData = response.getData();
                    if (null != responseData && null != responseData.getType() && !responseData.getType().isEmpty()) {
                        layoutAll.setVisibility(View.VISIBLE);
                        supplierTypeAdapter.setData(responseData.getType());
                    } else {
                        layoutAll.setVisibility(View.INVISIBLE);
                        setEmptyView(emptyView, null);
                    }
                } else {
                    layoutAll.setVisibility(View.INVISIBLE);
                    setEmptyView(emptyView, null);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                dismissWaiting();
                layoutAll.setVisibility(View.INVISIBLE);
                setEmptyView(emptyView, null);
            }
        });
        APP.getInstance().getRequestQueue().add(request);
    }

    public void onEventMainThread(SendMsgEvent event) {
        if (event.getSendType() == SendMsgEvent.CLOSE_ACTIVITY) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void emptyOnclick() {
        setHiddenView(emptyView);
        requestData();
    }
}
